import numpy as np
from jieba import analyse
import pandas as pd

df = pd.read_csv('static/data/info_pre.csv')
df_word = pd.read_csv('static/data/word_count.csv')


def word_cloud_plot_data():
    return {
        'data': [{'name': i[0], 'value': i[1]} for i in df_word.head(50).values.tolist()]
    }


def tag_count_rank_bar_plot():
    tag_list = '|'.join(df['标签'].values.tolist()).split('|')
    df_tag = pd.DataFrame(tag_list, columns=['tag'])
    data = df_tag.value_counts().reset_index().head(10).values.tolist()
    return {
        'label': [i[0] for i in data],
        'value': [i[1] for i in data]
    }


def every_year_play_list_count_rate_plot():
    data = df.groupby('年份')['歌单名称'].count().reset_index().values.tolist()
    return {
        'data': [{'name': i[0], 'value': i[1]} for i in data]
    }


def tags_collect_mean_rank_bar_plot():
    tags = set('|'.join(df['标签'].values.tolist()).split('|'))
    tags_mean_collect = [[i, round(df[df['标签'].str.contains(i)]['收藏'].mean())] for i in tags]
    df_collect_mean = pd.DataFrame(tags_mean_collect, columns=['tag', 'collect_mean'])
    data = df_collect_mean.sort_values('collect_mean', ascending=False).head(10).values.tolist()
    return {
        'label': [i[0] for i in data],
        'value': [i[1] for i in data]
    }


def play_list_radar_plot():
    columns = ['歌单名称', '收藏', '分享', '评论', '歌曲数量']
    data = df[columns].sort_values(by='收藏', ascending=False).values.tolist()
    data_max = [{'name': i, 'max': str(df[i].max())} for i in columns[1:]]
    return {
        'data': [{'name': i[0], 'value': i[1:]} for i in data],
        'max': data_max
    }

if __name__ == '__main__':
    print(play_list_radar_plot())